#include "myClipboard.h"
#include "clipboardManager.h"

#include <QApplication>
#include <QWidget>
#include <QScreen>
#include <QWindow>

#include <windows.h>
#include <QApplication>
#include <QDebug>
#include <QString>


RECT RetrieveMonitorBounds(int idx) {
    DISPLAY_DEVICEW dd;
    dd.cb = sizeof(dd);
    BOOL flag = EnumDisplayDevicesW(nullptr, idx, &dd, 0);

    DEVMODEW dm;
    dm.dmSize = sizeof(dm);
    dm.dmDriverExtra = 0;
    flag = EnumDisplaySettingsExW(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm, 0);

    RECT rect = { dm.dmPosition.x, dm.dmPosition.y, dm.dmPosition.x + dm.dmPelsWidth, dm.dmPosition.y + dm.dmPelsHeight };
    return rect;
}

// main函数

int main(int argc, char *argv[])
{

    int monitorCount = ::GetSystemMetrics(SM_CMONITORS);
    qDebug() << "Detected monitors: " << monitorCount;
    QString scalingFactors;

    for (int j = 0; j < monitorCount; ++j) {
        RECT dimensions = RetrieveMonitorBounds(j);
        int h = dimensions.bottom - dimensions.top;
        double scale = (h > 1080) ? (double)h / 1080.0 : 1.0;
        scalingFactors += (j == 0 ? "" : ";") + QString::number(scale, 'f', 1);
    }

    QByteArray envVar = scalingFactors.toUtf8();
    qputenv("QT_SCREEN_SCALE_FACTORS", envVar);

    // Enable high DPI scaling
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    // Optionally use high resolution pixmaps
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

    QApplication a(argc, argv);
    MyClipBoard w;

    // 获取主屏幕
    QScreen *screen = QGuiApplication::primaryScreen();
    QRect availableGeometry = screen->availableGeometry();
    
    // 设置窗口大小为可用屏幕空间的百分比
    int windowWidth = availableGeometry.width() * 0.2;
    int windowHeight = availableGeometry.height() * 0.58;
    
    // 设置窗口大小
    w.resize(windowWidth, windowHeight);
    
    // 计算窗口位置，确保完全显示在可用区域内
    int x = availableGeometry.right() - windowWidth - 80;
    int y = availableGeometry.top() + 50;
    
    w.move(x, y);
    w.show();

    return a.exec();
}
